#ifndef _EQTEST_TGA_LOADER_H
#define _EQTEST_TGA_LOADER_H

#include <stdint.h>

#include <memory>
#include <string>

namespace eqtest
{
    class TgaImage
    {
        public:
            explicit TgaImage(std::string const& path);
            
            uint16_t width() const
            { return header.image_spec.width; }
            
            uint16_t height() const
            { return header.image_spec.height; }
            
            uint8_t bits_per_pixel() const
            { return header.image_spec.bits_per_pixel; }
            
            uint8_t alpha_bits() const
            { return header.image_spec.alpha_bits; }
            
            uint8_t const* data() const
            { return pixel_data.get(); }
            
            void reverseRGB();
            
        private:
            struct TgaHeader
            {
                uint8_t id_len;
                uint8_t color_map_type;
                uint8_t image_type;
                struct __attribute__((packed))
                {
                    uint16_t first_entry_offset;
                    uint16_t num_entries;
                    uint8_t entry_size;
                } color_map_spec;
                struct __attribute__((packed))
                {
                    uint16_t x_orgin, y_orgin;
                    uint16_t width, height;
                    uint8_t bits_per_pixel;
                    uint8_t alpha_bits : 4;
                    uint8_t direction  : 2;
                    uint8_t padding    : 2;
                } image_spec;
            } __attribute__((packed)) header;
            
            std::unique_ptr<uint8_t[]> pixel_data;
    };
}

#endif
